package com.feng.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.feng.entity.User;
import com.feng.service.IUserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.feng.entity.Journal;
import com.feng.entity.JournalDetail;
import com.feng.service.IJournalService;
import com.feng.util.CommonUtils;
import com.feng.util.Dict;
import com.feng.util.PageUtil;
@Controller
public class JournalController {

	private final Log log=LogFactory.getLog(JournalController.class);
	@Autowired
	private IJournalService journalService;
	@Autowired
	private IUserService userService;
	
	@ResponseBody
	@RequestMapping("/testLog")
	public String testLog(){
		log.info("日志测试。。。。。。。。。。。。。。。");
		return null;
	}
	
	@RequestMapping("/blogIndex")
	public String blogIndex() {
		return "blogindex";
	}
	@ResponseBody
	@RequestMapping("/findJournalsJson")
	public List<Journal> findJournalsJson(HttpServletResponse response,HttpServletRequest request,String pageNum) throws Exception {
		List<Journal> journals=journalService.findAll();
		return journals;
	}
	@ResponseBody
	@RequestMapping("/findWorkJournalsJson")
	public List<Journal> findWorkJournalsJson(HttpServletResponse response,HttpServletRequest request,String pageNum) throws Exception {
		List<Journal> journals=journalService.findWorkJournals();
		return journals;
	}
	@ResponseBody
	@RequestMapping("/delJournal")
	public String delJournal(HttpServletRequest request,String id,String ckNum) throws Exception {
		String flag="0";
		String uname = (String)request.getSession().getAttribute(Dict.SESSION_USERNAME);
		User u = userService.findUser(uname,ckNum);
		if(u == null){
			return "0";
		}
		try {
			if("feng".equals(uname)) {
				journalService.deleteJournalInfo(id);
				flag = "1";
			}
		} catch (Exception e) {
			flag="0";
		}
		return flag;
	}
	@ResponseBody
	@RequestMapping("/findJournalsByTypeJson")
	public List<Journal> findJournalsByTypeJson(HttpServletRequest request,String type) throws Exception {
		String journalType="";
		if("work".equals(type)){
			journalType="1";
		}else if("book".equals(type)){
			journalType="2";
		}else if("sentence".equals(type)){
			journalType="3";
		}
		List<Journal> journals=journalService.findJournalsByType(journalType);
		return journals;
	}
	@ResponseBody
	@RequestMapping("/findJournalsByTypePageJson")
	public PageUtil<Journal> findJournalsByTypePageJson(Integer pageNum,String type) throws Exception {
		String journalType="";
		if("work".equals(type)){
			journalType="1";
		}else if("book".equals(type)){
			journalType="2";
		}else if("sentence".equals(type)){
			journalType="3";
		}
		PageUtil<Journal> journals=journalService.findJournalsByTypePage(journalType,pageNum);
		return journals;
	}
	@ResponseBody
	@RequestMapping("/saveJournal")
	public String saveJournal(String title,String content,String type){
		String flag="1";
		Journal journal=new Journal();
		journal.setContent(content);
		journal.setTitle(title);
		journal.setId(CommonUtils.getUUID());
		journal.setCredate(new Date());
		String journalType="";
		if("work".equals(type)){
			journalType="1";
		}else if("book".equals(type)){
			journalType="2";
		}else if("sentence".equals(type)){
			journalType="3";
		}
		journal.setType(journalType);
		try {
			journalService.saveJournal(journal);
			log.info(journal.getContent());
		} catch (Exception e) {
			flag="0";
			log.error(e.toString());
			e.printStackTrace();
		}
		
		return flag;
	}
	@ResponseBody
	@RequestMapping("/saveJournalDetail")
	public String saveJournalDetail(JournalDetail journalDetail){
		journalDetail.setCredate(new Date());
		journalDetail.setId(CommonUtils.getUUID());
	
		try {
			journalService.saveJournalDetail(journalDetail);
			log.info("journal_detail_content########"+journalDetail.getContent());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	@RequestMapping("/toJournalDetail")
	public String toJournalDetail(@RequestParam String id,@RequestParam String journalType,ModelMap modelMap){
		
		modelMap.addAttribute("id", id);
		modelMap.addAttribute("journalType",journalType);
		return "journalDetail";
	}
	@ResponseBody
	@RequestMapping("/findJournalByIdJson")
	public Map<String,Object> findJournalByIdJson(String id,HttpServletRequest request){
		Map<String,Object> resultMap=new HashMap<String,Object>();
		Journal journal=null;
		List<JournalDetail> journalDetails=null;
		try {
			 journal=journalService.findJournalById(id);
			 journalDetails=journalService.findJournalDetailsByJournalId(id);
		} catch (Exception e) {
			log.error(e.toString());
		}
		resultMap.put("journal", journal);
		resultMap.put("journalDetails", journalDetails);
		return resultMap;
	}
	@RequestMapping("/findJournalByTitle")
	public String findJournalByTitle(String title,HttpServletRequest request){
		return "journal";
	}
	@RequestMapping("/findJournalsByType")
	public String findJournalsByType(HttpServletRequest request,String type,String pageNum) throws Exception {
		return "classicStatement";
	}
	@ResponseBody
	@RequestMapping("deleteJournalDetail")
	public String deleteJournalDetail(String id){
		journalService.deleteJournalDetail(id);
		return null;
	}
}
